// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UILiveObjectDescriptorRemote.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.UI.Forms
{
    #region Using Directives

    using System;

    using Org.OpenTrader.Framework.Entities;
    using Org.OpenTrader.Framework.LiveObjects.Interfaces;

    #endregion

    /// <summary>
    /// The ui live object descriptor remote.
    /// </summary>
    public partial class UILiveObjectDescriptorRemote : UIControl
    {
        #region Constants and Fields

        /// <summary>
        /// The interface.
        /// </summary>
        private readonly string iface;

        /// <summary>
        /// The liveObject.
        /// </summary>
        private readonly DBLiveObject liveObject;

        /// <summary>
        /// The port.
        /// </summary>
        private int port = 1024 + Rand.NextInt(16000);

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="UILiveObjectDescriptorRemote"/> class.
        /// </summary>
        public UILiveObjectDescriptorRemote()
        {
            this.InitializeComponent();

            this.comboboxProtocol.SelectedIndex = 0;

            this.UpdateURLPreview();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UILiveObjectDescriptorRemote"/> class.
        /// </summary>
        /// <param name="iface">
        /// The iface.
        /// </param>
        /// <param name="liveObject">
        /// The liveObject.
        /// </param>
        public UILiveObjectDescriptorRemote(string iface, DBLiveObject liveObject)
        {
            this.InitializeComponent();

            this.iface = iface;
            this.liveObject = liveObject;

            // Parse URL
            if (this.liveObject.URL != null)
            {
                var remaining = this.liveObject.URL;
                var protocol = remaining.Substring(0, remaining.IndexOf(':'));
                remaining = remaining.Replace(protocol + "://", string.Empty);

                var host = remaining.Substring(0, remaining.IndexOf(':'));
                remaining = remaining.Replace(host + ":", string.Empty);

                var port = remaining.Substring(0, remaining.IndexOf('/'));
                remaining = remaining.Replace(port + "/", string.Empty);

                var service = remaining;

                this.comboboxProtocol.SelectedItem = protocol;
                this.textboxHost.Text = host;
                this.textboxPort.Text = port;
                this.textboxService.Text = service;
            }
            else
            {
                this.comboboxProtocol.SelectedIndex = 0;
            }

            // Update preview
            this.UpdateURLPreview();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets SelectedURL.
        /// </summary>
        public string SelectedURL
        {
            get
            {
                return string.Format(
                    "{0}://{1}:{2}/{3}", 
                    this.comboboxProtocol.SelectedItem as string, 
                    this.textboxHost.Text, 
                    this.textboxPort.Text, 
                    this.textboxService.Text);
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// The buttonApply_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnButtonApplyClick(object sender, EventArgs e)
        {
            // Push data
            this.liveObject.URL = this.SelectedURL;

            // HardOrSoftClosed the containing form (returns from Showdialog())
            if (this.ContainingForm != null)
            {
                this.ContainingForm.Destroy();
            }
        }

        /// <summary>
        /// The comboboxProtocol_ selected index changed.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnComboboxProtocolSelectedIndexChanged(object sender, EventArgs e)
        {
            this.UpdateURLPreview();
        }

        /// <summary>
        /// The textboxHost_ text changed.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnTextboxHostTextChanged(object sender, EventArgs e)
        {
            this.UpdateURLPreview();
        }

        /// <summary>
        /// The textboxPort_ text changed.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnTextboxPortTextChanged(object sender, EventArgs e)
        {
            int port;
            if (!int.TryParse(this.textboxPort.Text, out port))
            {
                this.textboxPort.Text = "17500";
            }

            this.UpdateURLPreview();
        }

        /// <summary>
        /// The textboxService_ text changed.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnTextboxServiceTextChanged(object sender, EventArgs e)
        {
            this.UpdateURLPreview();
        }

        /// <summary>
        /// The update url preview.
        /// </summary>
        private void UpdateURLPreview()
        {
            this.labelPreview.Text = this.SelectedURL;
        }

        #endregion
    }
}